//
//  UIViewController+Utility.m
//  pickpic
//
//  Created by Jinxiao on 8/23/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "UIViewController+PP.h"

@interface UIViewController (PP_)
@property (readwrite, nonatomic, strong) void (^leftAction) (void);
@property (readwrite, nonatomic, strong) void (^rightAction) (void);
@end

@implementation UIViewController (PP)

+ (void)load {
    [self swizzleMethod:@selector(viewWillAppear:) withMethod:@selector(swizzled_viewWillAppear:) error:nil];
    [self swizzleMethod:@selector(viewWillDisappear:) withMethod:@selector(swizzled_viewWillDisappear:) error:nil];
}

- (void (^)(void))leftAction {
    return objc_getAssociatedObject(self, "leftAction");
}

- (void)setLeftAction:(void (^)(void))leftAction {
    objc_setAssociatedObject(self, "leftAction", leftAction, OBJC_ASSOCIATION_COPY);
}

- (void (^)(void))rightAction {
    return objc_getAssociatedObject(self, "rightAction");
}

- (void)setRightAction:(void (^)(void))rightAction {
    objc_setAssociatedObject(self, "rightAction", rightAction, OBJC_ASSOCIATION_COPY);
}

- (UIBarButtonItem *)setNavigationLeftTitle:(NSString *)title action:(void (^)(void))action {
    return [self setNavigationLeftTitle:title titleColor:[UIColor whiteColor] action:action];
}

- (UIBarButtonItem *)setNavigationLeftTitle:(NSString *)title titleColor:(UIColor *)titleColor action:(void (^)(void))action {
    self.leftAction = action;

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(leftItemDidPressed:)];
    item.tintColor = titleColor;

    
    if (IS_IOS_8) {
            [item setTitleTextAttributes:@{NSFontAttributeName:PFFontMedium(16)} forState:UIControlStateNormal];
    }else{
    [item setTitleTextAttributes:@{NSFontAttributeName:PPFONT(PFBold, 16)} forState:UIControlStateNormal];
    }
    
    
    self.navigationItem.leftBarButtonItem = item;

    return item;
}

- (UIBarButtonItem *)setNavigationRightTitle:(NSString *)title action:(void (^)(void))action {
    return [self setNavigationRightTitle:title titleColor:[UIColor whiteColor] action:action];
}

- (UIBarButtonItem *)setNavigationRightTitle:(NSString *)title titleColor:(UIColor *)titleColor action:(void (^)(void))action {
    self.rightAction = action;

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleDone target:self action:@selector(rightItemDidPressed:)];
    item.tintColor = titleColor;

    
    if (IS_IOS_8) {
        [item setTitleTextAttributes:@{NSFontAttributeName:PFFontMedium(16)} forState:UIControlStateNormal];
    }else{
     [item setTitleTextAttributes:@{NSFontAttributeName:PPFONT(PFBold, 16)} forState:UIControlStateNormal];
    }
    
    self.navigationItem.rightBarButtonItem = item;
    

    return item;
}


- (UIBarButtonItem *)setNavigationRightImage:(UIImage *)image action:(void (^)(void))action {
    self.rightAction = action;

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(rightItemDidPressed:)];

    self.navigationItem.rightBarButtonItem = item;

    return item;
}

- (UIBarButtonItem *)setNavigationLefeImage:(UIImage *)image action:(void (^)(void))action {
    self.leftAction = action;

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftItemDidPressed:)];

    self.navigationItem.leftBarButtonItem = item;

    return item;
}

- (void)leftItemDidPressed:(UIBarButtonItem *)item {
    !self.leftAction ? : self.leftAction();
}

- (void)rightItemDidPressed:(UIBarButtonItem *)item {
    !self.rightAction ? : self.rightAction();
}

- (BOOL)prefersNavigationBarHidden {
    return NO;
}

- (void)swizzled_viewWillAppear:(BOOL)animated {
    [self swizzled_viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:[self prefersNavigationBarHidden] animated:animated];
}

- (void)swizzled_viewWillDisappear:(BOOL)animated {
    [self swizzled_viewWillDisappear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:animated];
}



- (void)backItemDidPressed {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSString *)preferredBackText {
    return nil;
}

- (UIImage *)preferredBackImage {
    return [UIImage imageNamed:@"return"];
}

- (UIBarButtonItem *)preferredBackItem {
    NSString *backText = [self preferredBackText];

    if(backText != nil) {
        return [[UIBarButtonItem alloc] initWithTitle:backText style:UIBarButtonItemStylePlain target:self action:@selector(backItemDidPressed)];
    } else {
        return [[UIBarButtonItem alloc] initWithImage:[self preferredBackImage] style:UIBarButtonItemStylePlain target:self action:@selector(backItemDidPressed)];
    }
}

@end
